use "Output Data\contracts_analysis_public.dta", clear

rename *, lower
drop if year < 2000

egen comp_agency = group(companyid agencyid)
egen agency_year = group(agencyid year)
egen comp_year = group(companyid year)
egen agency_ind = group(agencyid naics)
egen agency_ind_year = group(agencyid naics year)
bysort agencyid naics: egen ind_contracts = max(n_contracts)

tsset comp_agency year

gen log_ob = ln(total_obligation + 1)
replace log_ob = 0 if missing(log_ob)
gen contract_flag = 0
replace contract_flag = 1 if n_contracts > 0 & !missing(n_contracts)

gen event = 0
replace event = 1 if appointment == 1
replace event = 0 if year < 2000
replace event = 0 if year > 2018
bysort comp_agency: egen first_event_drop = min(year) if event == 1
bysort comp_agency: egen first_event = mean(first_event_drop)
drop first_event_drop
bysort comp_agency: egen first_year = min(year) if !missing(first_event)
replace first_year = 2000 if first_year < 2000
replace event = . if first_year > first_event - 2 & !missing(first_event) /*Psuedo drop these observations*/

gen event_tm2 = 0 if !missing(event)
replace event_tm2 = 1 if F2.event == 1 & !missing(event)
gen event_tm1 = 0 if !missing(event)
replace event_tm1 = 1 if F.event == 1 & !missing(event)
gen event_t = 0 if !missing(event)
replace event_t = 1 if event == 1 & !missing(event)
gen event_tp1 = 0 if !missing(event)
replace event_tp1 = 1 if L.event == 1 & !missing(event)
gen event_tp2 = 0 if !missing(event)
replace event_tp2 = 1 if L2.event == 1 & !missing(event)
gen event_tp3 = 0 if !missing(event)
replace event_tp3 = 1 if year > first_event + 2 & !missing(event)




reghdfe contract_flag event_tm2 event_tm1 event_t event_tp1 event_tp2 event_tp3 if year <= 2018 & !missing(connect_num) & ind_contracts > 0 & !missing(ind_contracts), a(agency_ind_year comp_year comp_agency) vce(cluster comp_agency)

merge m:1 companyid using "Output Data\contracts_analysis_public_nnmset.dta"
keep if _merge == 3
drop _merge

reghdfe contract_flag event_tm2 event_tm1 event_t event_tp1 event_tp2 event_tp3 if year <= 2018 & !missing(connect_num) & ind_contracts > 0 & !missing(ind_contracts), a(agency_ind_year comp_year comp_agency) vce(cluster comp_agency)
estimates store a1

reghdfe contract_flag event_tm2 event_tm1 event_t event_tp1 event_tp2 event_tp3 if year <= 2018 & !missing(connect_num) & _est_a1 == 1 & ind_contracts > 0 & !missing(ind_contracts), a(comp_agency) vce(cluster comp_agency)
estimates store m1

reghdfe contract_flag event_tm2 event_tm1 event_t event_tp1 event_tp2 event_tp3 if year >= 2000 & year <= 2018 & !missing(connect_num) & _est_a1 == 1 & ind_contracts > 0 & !missing(ind_contracts), a(agency_ind_year comp_year comp_agency) vce(cluster comp_agency)
estimates store m2

reghdfe log_ob event_tm2 event_tm1 event_t event_tp1 event_tp2 event_tp3 if year <= 2018 & !missing(connect_num) & _est_a1 == 1 & ind_contracts > 0 & !missing(ind_contracts), a(comp_agency) vce(cluster comp_agency)
estimates store m3

reghdfe log_ob event_tm2 event_tm1 event_t event_tp1 event_tp2 event_tp3 if year <= 2018 & !missing(connect_num) & _est_a1 == 1 & ind_contracts > 0 & !missing(ind_contracts), a(agency_ind_year comp_year comp_agency) vce(cluster comp_agency)
estimates store m4

#delimit ;
esttab m1 m2 m3 m4 output.csv, 
	starlevels(* 0.10 ** 0.05 *** 0.01)
	cells(b(star fmt(%9.6f)) t(par fmt(%9.2f)))
	ar2
	replace;
#delimit cr

sum contract_flag if _est_m1 == 1
sum log_ob if _est_m3 == 1